package com.example.classmanageapp;

import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;

import androidx.appcompat.app.AppCompatActivity;

import com.example.classmanageapp.utils.SysUtils;
import com.github.mikephil.charting.animation.Easing;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
import com.github.mikephil.charting.formatter.IValueFormatter;
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;
import com.github.mikephil.charting.utils.ViewPortHandler;

import java.util.ArrayList;

public class TasksActivity extends AppCompatActivity {

    private String[] types = {"数学", "英语", "计算机网络技术", "C语言", "Linux服务器操作系统", "算法与数据结构"};
    private float[] changes = {12.3f, 5.9f, 0.4f, 17.79f, 21.85f, 39.58f};
    private ImageButton img_back;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SysUtils.setStatusBarColor(this, true, 0);
        SysUtils.setSkin(this);
        setContentView(R.layout.activity_tasks);

        img_back = findViewById((R.id.img_back));
        img_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        BarChart mChart = findViewById(R.id.bar_chart);
        initBarChart(mChart);
        setBarChartData(types.length, mChart);
    }

    private void initBarChart(BarChart mBarChart) {
        mBarChart.setBackgroundColor(Color.WHITE);
        mBarChart.setDrawGridBackground(false); //网格
        mBarChart.getDescription().setEnabled(false);//描述
        //背景阴影
        mBarChart.setDrawBarShadow(false);

        //设置背景
        if (SysUtils.currentColor == 0) {
            /*设置当前Activity的背景色*/
            Resources res = this.getResources();
            Drawable drawable = res.getDrawable(R.drawable.main_back_color_red);
            mBarChart.setBackground(drawable);
        } else if (SysUtils.currentColor == 1) {
            /*设置当前Activity的背景色*/
            Resources res = this.getResources();
            Drawable drawable = res.getDrawable(R.drawable.main_back_color_blue);
            mBarChart.setBackground(drawable);
        }

        //显示边界
        mBarChart.setDrawBorders(false);

        //设置动画效果
        mBarChart.animateY(1000, Easing.EasingOption.Linear);
        mBarChart.animateX(1000, Easing.EasingOption.Linear);

        //折线图例 标签 设置
        Legend l = mBarChart.getLegend();
        l.setEnabled(false);

        YAxis leftAxis = mBarChart.getAxisLeft();
        YAxis rightAxis = mBarChart.getAxisRight();
        leftAxis.setAxisMinimum(0f);
        rightAxis.setAxisMinimum(0f);
        leftAxis.setEnabled(false);
        rightAxis.setEnabled(false);

        XAxis xAxis = mBarChart.getXAxis();

        //XY轴的设置
        //X轴设置显示位置在底部
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        //xAxis.setGranularity(1f);

        //xAxis.setDrawAxisLine(true);
        xAxis.setDrawGridLines(false);

        xAxis.setTextColor(0xff74828F);
        xAxis.setTextSize(10f);
        xAxis.setAxisLineColor(0xffe0e0e0);

        xAxis.setValueFormatter(new IAxisValueFormatter() {
            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                int idx = (int) value;
                return types[idx];
            }
        });
    }

    private void setBarChartData(int count, BarChart mChart) {
        ArrayList<BarEntry> yVals = new ArrayList<>();

        int[] colors = new int[count];

        for (int i = 0; i < count; i++) {
            float val = changes[i];

            if (val > 0) {
                colors[i] = 0xffF04933;
            }

            if (val < 0) {
                colors[i] = 0xff2BBE53;
            }

            yVals.add(new BarEntry(i, Math.abs(val)));
        }

        BarDataSet mBarDataSet = new BarDataSet(yVals, "股票数据");
        mBarDataSet.setDrawIcons(false);
        mBarDataSet.setColors(colors);
        mBarDataSet.setValueTextSize(12f);
        mBarDataSet.setValueTextColor(0xff74828F);

        ArrayList<IBarDataSet> dataSets = new ArrayList<>();
        dataSets.add(mBarDataSet);

        BarData mBarData = new BarData(dataSets);
        mBarData.setBarWidth(0.6f);

        mBarData.setValueFormatter(new IValueFormatter() {
            @Override
            public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) {
                int idx = (int) entry.getX();
                return String.valueOf(changes[idx]);
            }
        });

        mChart.setData(mBarData);
    }
}
